﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BeanOverflow.Models;

namespace BeanOverflow.Controllers
{
    public class ResponseController : Controller
    {
        private ResponseDBContext db = new ResponseDBContext();

        
        // GET: /Response/
        /// <summary>
        /// Displays the existing responses in tabular format
        /// </summary>
        /// <returns>a ViewResult containing all the existing responses
        /// </returns>
        [Authorize]
        public ViewResult Index()
        {
            return View(db.Responses.ToList());
        }

        
        // GET: /Response/Details/5
        /// <summary>
        /// Displays the details of the response corresponding to id
        /// </summary>
        /// <param name="id">ID of the response to be displayed</param>
        /// <returns>a ViewResult containing the response corresponding to id
        /// </returns>
        [Authorize]
        public ViewResult Details(int id)
        {
            Response response = db.Responses.Find(id);
            return View(response);
        }

        
        // GET: /Response/Create
        /// <summary>
        /// Displays an empty form where the user can provide input to the various fields
        /// </summary>
        /// <returns>an empty ActionResult</returns>
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        
        // POST: /Response/Create
        /// <summary>
        /// Receives newly created Response and stores it on the DB
        /// </summary>
        /// <param name="question">a newly created Question</param>
        /// <returns>an ActionResult view </returns>
        [HttpPost, Authorize]
        public ActionResult Create(Response response)
        {
            if (ModelState.IsValid)
            {
                db.Responses.Add(response);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(response);
        }

        
        // GET: /Response/Edit/5
        /// <summary>
        /// Displays a form where the user can provide modified the various fields of the Response corresponding to id
        /// </summary>
        /// <returns>an ActionResult</returns>
        [Authorize]
        public ActionResult Edit(int id)
        {
            Response response = db.Responses.Find(id);
            return View(response);
        }

        
        // POST: /Response/Edit/5
        /// <summary>
        /// Receives modified Response and stores it on the DB
        /// </summary>
        /// <param name="question">a recently modified Response</param>
        /// <returns>an ActionResult view </returns>
        [HttpPost, Authorize]
        public ActionResult Edit(Response response)
        {
            if (ModelState.IsValid)
            {
                db.Entry(response).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(response);
        }

        
        // GET: /Response/Delete/5
        /// <summary>
        /// Displays the details of the Response to be deleted corresponding to id
        /// </summary>
        /// <param name="id">id of the Response to be deleted</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Delete(int id)
        {
            Response response = db.Responses.Find(id);
            return View(response);
        }

        
        // POST: /Response/Delete/5
        /// <summary>
        /// Deletes the Response identified by id
        /// </summary>
        /// <param name="id">id of the Response to be deleted</param>
        /// <returns></returns>
        [HttpPost, ActionName("Delete"), Authorize]
        public ActionResult DeleteConfirmed(int id)
        {
            Response response = db.Responses.Find(id);
            db.Responses.Remove(response);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [Authorize]
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }


        /// <summary>
        /// Increases by one the vote count of the Response identified by id
        /// </summary>
        /// <param name="id">id of the Response which count will be increased</param>
        /// <returns></returns>
        [HttpPost, ActionName("VoteUp"), Authorize]
        public ActionResult VoteUp(int id)
        {
            Response res = db.Responses.Find(id);
            res.Votes = res.Votes + 1;
            int votes = res.Votes;
            db.Entry(res).State = EntityState.Modified;
            db.SaveChanges();
            return Json(votes);
        }

        /// <summary>
        /// Decreases by one the vote count of the Response identified by id
        /// </summary>
        /// <param name="id">id of the Response which count will be decreased</param>
        /// <returns></returns>
        [HttpPost, ActionName("VoteDown"), Authorize]
        public ActionResult VoteDown(int id)
        {
            Response res = db.Responses.Find(id);
            res.Votes = res.Votes - 1;
            int votes = res.Votes;
            db.Entry(res).State = EntityState.Modified;
            db.SaveChanges();
            return Json(votes);
        }
    }
}